import config
import time
from utils import *
from bubbles.randomWords import RandomWords


def register():
	return DailyWords()


class DailyWords:
	def __init__(self):
		self.type = 'active'
		self.last = [0 for x in range(len(config.bubbles.dailyWords.hours))]
		self.lastReview = 0
		self.rand = RandomWords()
		self.records = []
		self.help = \
'''每日单词：
	用法：无（自动）
	每日特定时间段将显示单词，并在晚上9点统一复习。'''

	def run(self, gum):
		now = time.time()
		for i in range(len(self.last)):
			if now - self.last[i] <= config.bubbles.dailyWords.gap \
					or time.localtime(now).tm_hour != config.bubbles.dailyWords.hours[i]:
				continue
			self.last[i] = now
			d = self.rand.randomWords(1)
			self.records.append(d)
			chain = MsgChain()
			chain.addPlain('锵锵~糖糖的每日单词来了哦：\n')
			chain.append(self.rand.makeMsg(d))
			sendFriendsAndGroups(chain, config.bubbles.dailyWords.friends, config.bubbles.dailyWords.groups)

		if now - self.lastReview <= config.bubbles.dailyWords.gap \
				or time.localtime(now).tm_hour != config.bubbles.dailyWords.reviewHour \
				or not self.records:
			return
		self.lastReview = now
		chain = MsgChain()
		chain.addPlain('来看看今天都学到了哪些单词吧！\n')
		chain.addPlain('糖糖想和木糖醇的大家们一起努力的说！~\(≧▽≦)/~\n')

		offset = 0
		for record in self.records:
			chain.append(self.rand.makeMsg(record, offset=offset))
			offset += len(record)

		self.records.clear()

		sendFriendsAndGroups(chain, config.bubbles.dailyWords.friends, config.bubbles.dailyWords.groups)
